package com.siwei.mes.common.jwt;

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Map;

/**
 * @author: mlchen
 * @createDate: 2022/8/5
 * @description: jwt token
 */
public class JwtUtils {
    private static String TOKEN = "jd-token-approval";

    /**
     * 生成token
     *
     * @param map 传入payload
     * @param day 过期时间  传-1 不过期
     * @return 返回token
     */
    public static String getToken(Map<String, String> map, Integer day) {
        JWTCreator.Builder builder = JWT.create();
        map.forEach(builder::withClaim);
        builder.withSubject(JSONObject.toJSONString(map));
        if (day > 0) {
            Calendar instance = Calendar.getInstance();
            instance.add(Calendar.DATE, day);
            builder.withExpiresAt(instance.getTime());
        }
        try {
            return builder.sign(Algorithm.HMAC256(TOKEN));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 验证token
     *
     * @param token
     * @return
     */
    public static void verify(String token) {
        try {
            JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 序列号token
     *
     * @param token
     * @return
     */
    public static String serializeToken(String token) {
        DecodedJWT decodedJWT = null;
        try {
            decodedJWT = JWT.require(Algorithm.HMAC256(TOKEN)).build().verify(token);
            return decodedJWT.getSubject();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ7XCJyb2xlXCI6XCLnrqHnkIblkZhcIixcInVzZXJQaG90b1wiOlwiaHR0cHM6Ly9zdGF0aWMtbGVnYWN5LmRpbmd0YWxrLmNvbS9tZWRpYS9sQURQRGlRM05GT2ZqbFROQWZUTkFmUV81MDBfNTAwLmpwZ1wiLFwidGFyZ2V0SWRcIjpcIjFcIixcImxvZ2luVHlwZVwiOlwiMlwiLFwicGhvbmVcIjpcIjEzNjYxNzU5NjkxXCIsXCJ0YXJnZXROb1wiOlwiWUgwMDFcIixcInVzZXJOYW1lXCI6XCLmnpflrZDnrqHnkIblkZhcIixcImRldmVsb3Blck5hbWVcIjpcIuaJrOW3nuW4guS9j-W7uuWxgOaciemZkOWFrOWPuFwiLFwidXNlcklkXCI6XCJZSDAwMVwiLFwidG9rZW5cIjpcIm51bGxcIn0iLCJyb2xlIjoi566h55CG5ZGYIiwidXNlclBob3RvIjoiaHR0cHM6Ly9zdGF0aWMtbGVnYWN5LmRpbmd0YWxrLmNvbS9tZWRpYS9sQURQRGlRM05GT2ZqbFROQWZUTkFmUV81MDBfNTAwLmpwZyIsInRhcmdldElkIjoiMSIsImxvZ2luVHlwZSI6IjIiLCJwaG9uZSI6IjEzNjYxNzU5NjkxIiwidGFyZ2V0Tm8iOiJZSDAwMSIsInVzZXJOYW1lIjoi5p6X5a2Q566h55CG5ZGYIiwiZGV2ZWxvcGVyTmFtZSI6IuaJrOW3nuW4guS9j-W7uuWxgOaciemZkOWFrOWPuCIsInVzZXJJZCI6IllIMDAxIiwidG9rZW4iOiJudWxsIn0.c8Tmy7ySRHiEFCBPbETFwt75Xcr1VR-rTbByMx3Qr-s";
        String subject = JwtUtils.serializeToken(token);
//        LoginDto user = JsonUtils.getObject(subject, LoginDto.class);
//        System.out.println(user);
    }
}
